package com.huage_01.lesson_01.Junit;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.lang.reflect.Method;

/*
        解析注解
 */
public class CheckDeal {
    public static void main(String[] args) throws IllegalAccessException, InstantiationException, IOException {
        Class<Calculator> c = Calculator.class;

        BufferedWriter bw = new BufferedWriter(new FileWriter("bug.txt"));
        Calculator obj = c.newInstance();
        Method[] dMethods = c.getDeclaredMethods();
        int num = 0;
        for (Method method : dMethods){
            if (method.isAnnotationPresent(Check.class)){
                method.setAccessible(true);
                try {
                    method.invoke(obj);
                }catch (Exception e){
                     num++;
                     bw.write(method.getName()+"方法出现异常");
                     bw.newLine();
                    bw.write("异常名称:"+e.getCause().getClass().getSimpleName());
                    bw.newLine();
                    bw.write("异常原因:"+e.getCause().getMessage());
                    bw.newLine();
                    bw.write("--------");
                    bw.flush();
                }

            }
        }
        bw.write("本次出现"+num+"次异常");
        bw.flush();
        bw.close();
    }
}
